// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------
// Modification history:
// feature: DServer support simple entity
// feature: discovery support client and server
// feature: add statictic lookup
// ------------------------------------------------------------------

#ifndef _EDDS_ENTITYRTPS_COMMON_ENTITYID_T_HPP_
#define _EDDS_ENTITYRTPS_COMMON_ENTITYID_T_HPP_

#include <ertps/ertps_dll.h>
#include <edds/rtps/common/Types.h>
#include <deps/common/EntityId_t.hpp>

#include <cstdint>
#include <cstring>
#include <sstream>

namespace evbs {
namespace ertps {
namespace rtps {

#define ENTITYID_RTPSParticipant 0x000001c1U
#define ENTITYID_SEDP_BUILTIN_TOPIC_WRITER 0x000002c2U
#define ENTITYID_SEDP_BUILTIN_TOPIC_READER 0x000002c7U
#define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER 0x000003c2U
#define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_READER 0x000003c7U
#define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_WRITER 0x000004c2U
#define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_READER 0x000004c7U
#define ENTITYID_SPDP_BUILTIN_RTPSParticipant_WRITER 0x000100c2U
#define ENTITYID_SPDP_BUILTIN_RTPSParticipant_READER 0x000100c7U
// #define ENTITYID_SPDP_BUILTIN_RTPSParticipant_COMPATIBLE_WRITER 0x00010042U
#define ENTITYID_SPDP_BUILTIN_RTPSParticipant_COMPATIBLE_READER 0x00010047U
#define ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_WRITER 0x000200C2U
#define ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_READER 0x000200C7U
#define ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_WRITER 0x000201C3U
#define ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_READER 0x000201C4U

#define ENTITYID_TL_SVC_REQ_WRITER 0x000300C3
#define ENTITYID_TL_SVC_REQ_READER 0x000300C4
#define ENTITYID_TL_SVC_REPLY_WRITER 0x000301C3
#define ENTITYID_TL_SVC_REPLY_READER 0x000301C4

#if HAVE_SECURITY
#define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_WRITER 0xff0003c2
#define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_READER 0xff0003c7
#define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_WRITER 0xff0004c2
#define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_READER 0xff0004c7
#define ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_WRITER 0xff0200c2
#define ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_READER 0xff0200c7
#define ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_WRITER 0xff0202C3
#define ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_READER 0xff0202C4
#define ENTITYID_SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_WRITER 0xff0101c2
#define ENTITYID_SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_READER 0xff0101c7
#endif  // if HAVE_SECURITY

#define ENTITYID_DS_SERVER_VIRTUAL_WRITER 0x00030073
#define ENTITYID_DS_SERVER_VIRTUAL_READER 0x00030074

using EntityId_t = vbsutil::xmlparser::EntityId_t;

const EntityId_t c_EntityId_Unknown = ENTITYID_UNKNOWN;
const EntityId_t c_EntityId_SPDPReader = ENTITYID_SPDP_BUILTIN_RTPSParticipant_READER;
const EntityId_t c_EntityId_SPDPWriter = ENTITYID_SPDP_BUILTIN_RTPSParticipant_WRITER;

// const EntityId_t c_EntityId_Compatible_SPDPWriter = ENTITYID_SPDP_BUILTIN_RTPSParticipant_COMPATIBLE_WRITER;
const EntityId_t c_EntityId_Compatible_SPDPReader = ENTITYID_SPDP_BUILTIN_RTPSParticipant_COMPATIBLE_READER;

const EntityId_t c_EntityId_SEDPPubWriter = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER;
const EntityId_t c_EntityId_SEDPPubReader = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_READER;
const EntityId_t c_EntityId_SEDPSubWriter = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_WRITER;
const EntityId_t c_EntityId_SEDPSubReader = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_READER;

const EntityId_t c_EntityId_RTPSParticipant = ENTITYID_RTPSParticipant;

const EntityId_t c_EntityId_WriterLiveliness = ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_WRITER;
const EntityId_t c_EntityId_ReaderLiveliness = ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_READER;

const EntityId_t participant_stateless_message_writer_entity_id = ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_WRITER;
const EntityId_t participant_stateless_message_reader_entity_id = ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_READER;

const EntityId_t c_EntityId_Lookup_request_writer = ENTITYID_TL_SVC_REQ_WRITER;
const EntityId_t c_EntityId_Lookup_request_reader = ENTITYID_TL_SVC_REQ_READER;
const EntityId_t c_EntityId_Lookup_reply_writer = ENTITYID_TL_SVC_REPLY_WRITER;
const EntityId_t c_EntityId_Lookup_reply_reader = ENTITYID_TL_SVC_REPLY_READER;

#if HAVE_SECURITY
const EntityId_t sedp_builtin_publications_secure_writer = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_WRITER;
const EntityId_t sedp_builtin_publications_secure_reader = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_READER;
const EntityId_t sedp_builtin_subscriptions_secure_writer = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_WRITER;
const EntityId_t sedp_builtin_subscriptions_secure_reader = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_READER;

const EntityId_t participant_volatile_message_secure_writer_entity_id =
    ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_WRITER;
const EntityId_t participant_volatile_message_secure_reader_entity_id =
    ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_READER;

const EntityId_t c_EntityId_WriterLivelinessSecure = ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_WRITER;
const EntityId_t c_EntityId_ReaderLivelinessSecure = ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_READER;
const EntityId_t c_EntityId_spdp_reliable_participant_secure_reader =
    ENTITYID_SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_READER;
const EntityId_t c_EntityId_spdp_reliable_participant_secure_writer =
    ENTITYID_SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_WRITER;
#endif  // if HAVE_SECURITY

const EntityId_t ds_server_virtual_writer = ENTITYID_DS_SERVER_VIRTUAL_WRITER;
const EntityId_t ds_server_virtual_reader = ENTITYID_DS_SERVER_VIRTUAL_READER;
}  // namespace rtps
}  // namespace ertps
}  // namespace evbs

namespace std {
template <>
struct hash<evbs::ertps::rtps::EntityId_t> {
    std::size_t operator()(const evbs::ertps::rtps::EntityId_t& k) const {
        return (static_cast<size_t>(k.value[0]) << 16) | (static_cast<size_t>(k.value[1]) << 8) |
               static_cast<size_t>(k.value[2]);
    }
};

}  // namespace std

#endif /* _EDDS_ENTITYRTPS_COMMON_ENTITYID_T_HPP_ */
